package com.sulent.frame.file.impl;

import com.sulent.frame.file.FileServer;
import com.sulent.frame.file.dto.FileVo;
import com.sulent.frame.file.utils.MinioUtils;
import io.minio.DownloadObjectArgs;
import io.minio.MinioClient;
import io.minio.errors.*;
import lombok.Data;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * @ClassName MinioFileServer
 * @Description: TODO
 * @Author Submerge.
 * @Since 2024/1/18 10:25
 * @Version 1.0
 */
@Data
public class MinioFileServer implements FileServer {

    private String endpoint;
    private String accessKey;
    private String secretKey;
    private String bucketName;

    public MinioFileServer(String endpoint, String accessKey, String secretKey,String bucketName) {
        this.endpoint = endpoint;
        this.accessKey = accessKey;
        this.secretKey = secretKey;
        this.bucketName = bucketName;
    }

    @Override
    public void uploadFile(File file, String targetPath) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public File downloadFile(String sourceFilePath, String destinationPath) {
        MinioClient minioClient = MinioUtils.connectionMinio(endpoint, accessKey, secretKey);
        try {
            minioClient.downloadObject(DownloadObjectArgs.builder().filename(destinationPath).object(sourceFilePath).bucket("ai-model").overwrite(true).build());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<FileVo> listFiles(String targetPath) {
        MinioClient minioClient = MinioUtils.connectionMinio(endpoint, accessKey, secretKey);
        return MinioUtils.listFiles(minioClient,bucketName, targetPath);
    }

    public List<FileVo> pageFiles(String targetPath,String startAfter) {
        MinioClient minioClient = MinioUtils.connectionMinio(endpoint, accessKey, secretKey);
        return MinioUtils.pageFiles(minioClient,bucketName, targetPath,startAfter);
    }

    @Override
    public void close() throws Exception {
        System.out.println("minio file sever close");
    }
}
